// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
// require('es6-promise').polyfill()
import Vue from 'vue'
import App from './App'
import router from './router'
// import 'babel-polyfill'
import 'normalize.css/normalize.css' // A modern alternative to CSS resets
// import 'promise-polyfill/src/polyfill'
import '@/plugins/flexible'
import MintUI from 'mint-ui'
import store from '@/store'
// import LyTab from 'ly-tab'
import AwesomePicker from 'vue-awesome-picker'
import vueTabbarSlide from 'vue-tabbar-slide'
// import FastClick from "fastclick";
import './filters'
import '@/utils/directives'

import VueScroller from 'vue-scroller'
// import VeeValidate from "vee-validate"
import '@/plugins/validator/validator.js' // vue验证
// import VueInput from "vue-input"; //解决移动端input框被输入法面新版遮挡的问题
// Vue.use(VueInput);
// import VCconsole from 'vconsole'
// icon
require('@/assets/common/fonts/iconfont.css')
require('vue2-animate/dist/vue2-animate.min.css')

// if ("addEventListener" in document) {
//   document.addEventListener(
//     "DOMContentLoaded",
//     function() {
//       FastClick.attach(document.body);
//     },
//     false
//   );
// }

Vue.use(MintUI)
Vue.use(VueScroller)
// Vue.use(VCconsole)
// Vue.use(LyTab)
Vue.use(AwesomePicker)
Vue.use(vueTabbarSlide)

import { getScrollTop, setScrollTop, getSessionStorage } from '@/utils/mixin'
const routerList = []
router.beforeEach((to, from, next) => {
  // 返回上一级浏览位置
  const position = getScrollTop()
  const currentRouterIndex = routerList.findIndex(e => {
    return e.path === from.fullPath
  })

  if (currentRouterIndex !== -1) {
    routerList[currentRouterIndex].position = position
  } else {
    routerList.push({
      path: from.fullPath,
      position: position
    })
  }
  // // next()
  // // console.log( from.fullPath )
  // // console.log(to)
  // if (to.path === '/login') {
  //   to.query.redirect = from.fullPath
  // }
  //  if (to.meta.requiresAuth && !islogin) { // 需要登录授权，这里还需要判断是否登录
  //   next('/login') // 跳转到登录
  //   return
  // }
  // // 路由控制
  // const token = getSessionStorage('b_token')
  // if (token) {
  //   store.dispatch('GetUserInfo', { user_token: token }).then(res => {
  //     next()
  //   })
  // } else {
  //   next()
  // }
  const token = getSessionStorage('b_token')
  if (token) {
    if (to.path === '/login') {
      next({ path: '/' })
    } else {
      store.dispatch('GetUserInfo', { user_token: token }).then(res => {
        // 拉取用户信息
        next()
      })
      // if (store.getters.roles.length === 0) {
      //   store
      //     .dispatch('GetInfo')
      //     .then(res => {
      //       // 拉取用户信息
      //       next()
      //     })
      //     .catch(err => {
      //       store.dispatch('FedLogOut').then(() => {
      //         Message.error(err || 'Verification failed, please login again')
      //         next({ path: '/' })
      //       })
      //     })
      // } else {
      //   next()
      // }
    }
  } else {
    if (to.meta.requiresAuth && to.path !== '/login') {
      // to.query.redirect = to.fullPath
      // next('/login')
      next({
        path: '/login',
        query: { redirect: to.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    } else {
      // redirect: "/security"
      // 如果从购物车，home点击登录按钮后返回该页面
      // if (from.path === '/cart' || from.path === '/home') {
      //   to.query.redirect = from.fullPath
      // }
      // if (!to.meta.requiresAuth && to.path !== '/login') {
      //   to.query.redirect = from.fullPath
      // }

      // if (
      //   !to.meta.requiresAuth &&
      //   (from.path === '/cart' || from.path === '/home')
      // ) {
      //   next({
      //     path: to.path,
      //     query: { redirect: from.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
      //   })

      //   //   to.query.redirect = from.fullPath
      // }
      next()
    }

    // if (to.meta.requiresAuth && to.path !== '/login') {
    //   // 要去的页面要求登录，登录后定向到要去的页面
    //   to.query.redirect = to.fullPath
    //   console.log('auth')
    //   next('/login')
    // } else {
    //   if (to.path === '/login') {
    //     to.query.redirect = from.fullPath
    //     console.log(to)
    //   }
    //   next()
    // }
  }
})

router.afterEach((to, from, next) => {
  const savedPosition = routerList.find(e => {
    return e.path === to.fullPath
  })

  if (typeof savedPosition !== 'undefined') {
    Vue.nextTick(() => {
      setScrollTop(savedPosition.position)
    })
  } else {
    Vue.nextTick(() => {
      setScrollTop(0)
    })
  }
})

Vue.config.productionTip = false

// /* fundebug */
// function formatComponentName(vm) {
//   if (vm.$root === vm) return 'root'

//   var name = vm._isVue
//     ? (vm.$options && vm.$options.name) ||
//       (vm.$options && vm.$options._componentTag)
//     : vm.name
//   return (
//     (name ? 'component <' + name + '>' : 'anonymous component') +
//     (vm._isVue && vm.$options && vm.$options.__file
//       ? ' at ' + (vm.$options && vm.$options.__file)
//       : '')
//   )
// }

// Vue.config.errorHandler = function(err, vm, info) {
//   var componentName = formatComponentName(vm)
//   var propsData = vm.$options && vm.$options.propsData

//   fundebug.notifyError(err, {
//     metaData: {
//       componentName: componentName,
//       propsData: propsData,
//       info: info
//     }
//   })
// }

/* eslint-disable no-new */
Vue.prototype.errorEvent = e => {
  e.target.src = './assets/images/logo.png'
}
new Vue({
  el: '#app',
  store,
  router,
  template: '<App/>',
  components: { App }
})
